layui.use(['customUtil'], function (customUtil) {

    var table = layui.table;
    var form = layui.form;
    form.render();
    var searchKeyword = "";
    var orderStatus = "0";
    // var taskMap = new Map();
    // var setIntervalTask;
    var seckillOrders = {

        _constant: {
            api: {
                querySeckillOrders: appCtx + '/seckillOrder/queryOrders',
                payOrder: appCtx + '/seckillOrder/payOrder',
                cancelOrder: appCtx + '/seckillOrder/cancelOrder'
            },
            taskMap : new Map(),
            setIntervalTask : null
        },

        _init: function () {
            let _this = this;
            _this._bind();
            _this._beforeRenderTable();
            _this._tableTool();
        },

        _bind: function () {
            let _this = this;
            $("input:radio[name=orderStatus]").on("change", function (event) {
                _this._beforeRenderTable();
            });
        },

        _beforeRenderTable: function () {
            //taskMap = new Map();
            let _this = this;
            _this._constant.taskMap = new Map();
            orderStatus = $('input:radio[name=orderStatus]:checked').val();
            if (orderStatus === "0") {
                let columns = [[ //表头
                    {field: 'seckillName', title: '商品名称'},
                    {
                        field: 'remainTime', title: '付款倒计时', templet: function (data) {
                            let minute = Math.floor(data.remainTime / 60);
                            let second = data.remainTime % 60;
                            let labelKey = "key_" + data.seckillId;
                            let html = '<label id="' + labelKey + '" style="color: red;">' + minute + ' 分钟 ' + second + ' 秒</label>';
                            _this._addTask(data.seckillId, data);
                            return html;
                        }
                    },
                    {
                        fixed: 'right',
                        width: 165,
                        align: 'center',
                        toolbar: '#seckillOrdersTableBar'
                    }
                ]];
                _this._renderTable(columns);
            } else {
                let columns = [[ //表头
                    {field: 'name', title: '商品名称'},
                    {field: 'startTime', title: '付款倒计时'},
                    {
                        fixed: 'right',
                        width: 165,
                        align: 'center',
                        toolbar: '#seckillOrdersTableBar'
                    }
                ]];
            }
            _this._doTask();
        },

        _renderTable: function (column) {
            let _this = this;
            //执行一个 table 实例
            table.render({
                elem: '#seckillOrderTable',
                url: _this._constant.api.querySeckillOrders, //数据接口
                method: 'post', //如果无需自定义HTTP类型，可不加该参数
                where: {
                    "seckillGoodsName": searchKeyword,
                    "orderStatus": orderStatus
                },
                title: '订单表',
                toolbar: 'default', //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                totalRow: false,//开启合计行
                cols: column
            });
        },

        _tableTool: function () {
            let _this = this;
            //监听行工具事件
            table.on('tool(seckillOrdersTableBar)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                let data = obj.data, //获得当前行数据
                    layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'payOrder') {
                    layer.confirm('真的取消么', function () {
                        _this._payOrder(data.seckillId);
                    });
                } else if (layEvent === 'cancelOrder') {
                    layer.confirm('真的取消么', function () {
                        _this._cancelOrder(data.seckillId);
                    });
                }
            });
        },

        _payOrder: function (seckillId) {
            let param = {
                "seckillId": seckillId
            };
            customUtil.post(_this._constant.api.payOrder, param, function (data) {
                if (data.success) {
                    if (data.result) {
                        layer.msg(data.result.msg);
                    }
                } else {
                    layer.msg(data.messages, {
                        title: '操作失败'
                    })
                }
            });
        },

        _cancelOrder: function (seckillId) {
            let param = {
                "seckillId": seckillId
            };
            customUtil.post(_this._constant.api.cancelOrder, param, function (data) {
                if (data.success) {
                    if (data.result) {
                        layer.msg(data.result.msg);
                    }
                } else {
                    layer.msg(data.messages, {
                        title: '操作失败'
                    })
                }
            });
        },

        _doTask: function () {
            // 定时计时任务，这里是1秒执行一次
            let _this = this;
            if (_this._constant.setIntervalTask) {
                clearInterval(_this._constant.setIntervalTask);
            }
            _this._constant.setIntervalTask = setInterval(function () {
                    for (var key in _this._constant.taskMap) {
                        _this._setRemainTime(_this._constant.taskMap[key]);
                    }
                }, 1000
            )
        },

        _setRemainTime: function (data) {
            let _this = this;
            data.remainTime--;
            let labelKey = "key_" + data.seckillId;
            if (!data.remainTime) {
                $('#' + labelKey).text("00分钟00秒");
                _this._delTask(data.seckillId);
            }
            let minute = Math.floor(data.remainTime / 60);
            let second = data.remainTime % 60;
            $('#' + labelKey).text(minute + '分钟' + second + '秒');
        },

        // 添加定时任务
        _addTask: function (key, value) {
            let _this = this;
            _this._constant.taskMap[key] = value;
        },

        // 删除定时任务
        _delTask: function (key) {
            delete taskMap[key];
        }
    };
    seckillOrders._init();
});